import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: function () {
      return import('@/views/About.vue')
    },
    meta: 'About的元信息'
  },
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('@/views/Login.vue')
  }
]

const router = new VueRouter({

  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savePosition){
    console.log(savePosition);
    return {
      x:0,
      y:0
    }
  }
})

let loginStatue = false;
//路由前置守卫
router.beforeEach((to, from, next) =>{
  if (loginStatue){
      next();
  }else {
    loginStatue = true
    next('/login')
  }
})

router.afterEach((to, from) => {
  console.log('收尾工作');
})


export default router
